package dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DataBaseConnection;

import bean.Departamento;

import dao.Entidad;


public class MySQLDepartamentoDAO implements Entidad<Departamento> {

	
	@Override
	public Departamento insert(Departamento t) {
		/*Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO Departamento(id_Departamento,id_Pais,nombre";
			sql = sql + ") VALUES(?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setInt(1, t.getIdDepartamento());
			ps.setInt(2, t.getIdPais());
			ps.setString(3, t.getNombreDepartamento());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setIdDepartamento(id);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return t;
	}

	@Override
	public int update(Departamento t) {
		/*Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE Departamento SET nombre=?";
			sql = sql +	"WHERE id_Depatamento=? and id_Pais=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setString(1, t.getNombreDepartamento());
			ps.setInt(2, t.getIdDepartamento());
			ps.setInt(3, t.getIdPais());
					
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public int delete(Departamento t) {
		/*Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="DELETE FROM Departamento WHERE id_Departamento=? and id_Pais=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getIdDepartamento());
			ps.setInt(2, t.getIdPais());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return -1;
	}

	@Override
	public Departamento findById(Departamento t) {
		/*Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM Departamento d, Pais p WHERE d.id_Departamento=? and d.id_Pais = p.id_Pais";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getIdDepartamento());
			ResultSet rs=ps.executeQuery();
			
			while(rs.next()){
				t.setIdDepartamento(rs.getInt("id_Departamento"));
				t.setNombreDepartamento(rs.getString("nombre"));
				t.setIdPais(rs.getInt("id_Pais"));
				t.setNombrePais(rs.getString("nombre"));		
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return t;
	}

	@Override
	public List<Departamento> listAll() {
		/*List<Departamento> departamento = new ArrayList<Departamento>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM Departamento";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Departamento t = new Departamento();
				t.setIdDepartamento(rs.getInt("id_departamento"));
				t.setNombreDepartamento(rs.getString("nombre"));
				t.setIdPais(rs.getInt("id_Pais"));
				t.setNombrePais(rs.getString("nombre"));
				departamento.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}*/
		return null;
	}

	@Override
	public List<Departamento> listAllCustom(Object... objects) {
		String id = (String)objects[0];
		List<Departamento> departamento = new ArrayList<Departamento>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM Departamento d WHERE d.id_pais = '"+id+"' order by d.nomb_depa asc";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Departamento t = new Departamento();
				t.setId_depa(rs.getInt("id_depa"));
				t.setNomb_depa(rs.getString("nomb_depa"));
				t.setId_pais(rs.getInt("id_pais"));
				departamento.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return departamento;
	}
	
}
